<template>
  <!-- 授权用户 -->
  <el-dialog
    title="选择菜单"
    v-model="visible"
    width="800px"
    top="5vh"
    append-to-body
  >
 
    <el-row>
      <el-table
        @row-click="clickRow"
        ref="refTable"
        :data="userList"
         row-key="id"
          :default-selected-keys="[1,2]"
        @selection-change="handleSelectionChange"
        height="260px"
      >
      <el-table-column type="selection"
    :reserve-selection="true"
      width="55"></el-table-column>
      
      <el-table-column label="编号" align="left" prop="id" :show-overflow-tooltip="true" sortable />

      <el-table-column label="菜单中文名" align="center" prop="description" />

      <el-table-column label="pid" align="center" prop="pid" />

      <el-table-column label="grade类型" align="center" prop="grade" />

      <el-table-column label="排序" align="center" prop="orders" />

      <el-table-column label="路径path" align="center" prop="path" />

      <el-table-column label="组件路径" align="center" prop="component"  width="250"/>

      <el-table-column label="菜单权限" align="center" prop="remark" />

      <el-table-column label="按钮权限" align="center" prop="permi" />
      </el-table>
 
    </el-row>
   
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSelectUser">确 定</el-button>
        <el-button @click="visible = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="SelectUser">
import {
  listAllMenu,
} from "@/api/business/menu";

import {
    listRoleMenu,
    addRoleMenu,
    delRoleMenu,
  } from "@/api/business/roleMenu";
import { nextTick } from "vue";
 
 

const props = defineProps({
  roleId: {
    type: [Number, String],
  },
  menu: {
    type: Array,
    default() {
      return []
    }
  },
});
const route = useRoute();
const { proxy } = getCurrentInstance();

const userList = ref([]);
const visible = ref(false);
const total = ref(0);
const userIds = ref([]);
const selectedIds = ref([]);
const refTable = ref(null)

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
 
});

const defaultSelected = ref([userList.value[0]]);

const multipleSelection = ref([1])



const handleTest = ()=>{

  let arr = userList.value.filter((item)=>    props.menu.includes(item.id) )

  arr.forEach((item)=>{
    refTable.value?.toggleRowSelection(item, true)
  })

}


// 显示弹框
function show() {
  
  visible.value = true;
  getList();
}
/**选择行 */
function clickRow(row) {
  proxy.$refs["refTable"].toggleRowSelection(row);
}
// 多选框选中数据
function handleSelectionChange(selection) {
  userIds.value = selection.map((item) => item.id);
}
// 查询表数据
function getList() {
  listAllMenu(queryParams).then((response) => {
    userList.value = response.data;
    handleTest()
    total.value = response.data.count;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
const emit = defineEmits(["ok"]);
/** 选择授权用户操作 */
function handleSelectUser() {
  const roleId = route.params.roleId;


  const uIds = userIds.value.join(",");



  if (uIds == "") {
    proxy.$modal.msgError("请选择要分配的用户");
    return;
  }
  let sendData = [];
  sendData = userIds.value.map((item) => {
    return {
      rid: roleId,
      mid: item,
    };
  });
  addRoleMenu({ ids: sendData }).then((res) => {
    proxy.$modal.msgSuccess(res.msg);
    if (res.code === 200) {
      visible.value = false;
      emit("ok");
    }
  });
}

 
 
defineExpose({
  show,
});
</script>
